using UnityEngine;
using System.Collections.Generic;

public class AudioManager : MonoBehaviour
{
    public static AudioManager Instance;

    [System.Serializable]
    public class Sound
    {
        public string name;
        public AudioClip clip;
        [Range(0f, 1f)]
        public float volume = 1f;
        [Range(0.1f, 3f)]
        public float pitch = 1f;
        public bool loop = false;

        [HideInInspector]
        public AudioSource source;
    }

    public List<Sound> musicTracks = new List<Sound>();
    public List<Sound> soundEffects = new List<Sound>();

    private bool musicOn = true;
    private bool soundEffectsOn = true;
    private bool vibrationOn = true;

    // 公共属性来获取音乐和音效的开关状态
    public bool IsMusicOn => musicOn;
    public bool IsSoundEffectsOn => soundEffectsOn;
    public bool IsVibrationOn => vibrationOn;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
            return;
        }

        foreach (Sound s in musicTracks)
        {
            s.source = gameObject.AddComponent<AudioSource>();
            s.source.clip = s.clip;
            s.source.volume = s.volume;
            s.source.pitch = 1;
            s.source.loop = s.loop;
        }

        foreach (Sound s in soundEffects)
        {
            s.source = gameObject.AddComponent<AudioSource>();
            s.source.clip = s.clip;
            s.source.volume = s.volume;
            s.source.pitch = 1;
            s.source.loop = s.loop;
        }

        LoadSettings();
    }

    private void LoadSettings()
    {
        musicOn = PlayerPrefs.GetInt("MusicOn", 1) == 1;
        soundEffectsOn = PlayerPrefs.GetInt("SoundEffectsOn", 1) == 1;
        vibrationOn = PlayerPrefs.GetInt("VibrationOn", 1) == 1;
    }

    private void SaveSettings()
    {
        PlayerPrefs.SetInt("MusicOn", musicOn ? 1 : 0);
        PlayerPrefs.SetInt("SoundEffectsOn", soundEffectsOn ? 1 : 0);
        PlayerPrefs.SetInt("VibrationOn", vibrationOn ? 1 : 0);
        PlayerPrefs.Save();
    }

    public void ToggleMusic()
    {
        musicOn = !musicOn;
        SaveSettings();
        if (!musicOn)
        {
            StopAllMusic();
        }
        else
        {
            PlayMusic("bgmusic");
        }
    }

    public void ToggleSoundEffects()
    {
        soundEffectsOn = !soundEffectsOn;
        SaveSettings();
    }

    public void ToggleVibration()
    {
        vibrationOn = !vibrationOn;
        SaveSettings();
    }

    public void SetMusicOn(bool isOn)
    {
        musicOn = isOn;
        SaveSettings();
        if (!musicOn)
        {
            StopAllMusic();
        }
    }

    public void SetSoundEffectsOn(bool isOn)
    {
        soundEffectsOn = isOn;
        SaveSettings();
    }

    public void PlayMusic(string name)
    {
        Debug.Log($"Attempting to play music: {name}. Music on: {musicOn}");
        if (!musicOn) return;
        Sound s = musicTracks.Find(sound => sound.name == name);
        if (s == null)
        {
            Debug.LogWarning("Music: " + name + " not found!");
            return;
        }
        Debug.Log($"Found music track: {s.name}. Volume: {s.volume}, Pitch: {s.pitch}");
        s.source.Play();
        Debug.Log($"Played music: {name}. Is playing: {s.source.isPlaying}");
    }

    public void PlaySoundEffect(string name)
    {
        if (!soundEffectsOn) return;
        Sound s = soundEffects.Find(sound => sound.name == name);
        if (s == null)
        {
            Debug.LogWarning("Sound Effect: " + name + " not found!");
            return;
        }
        s.source.PlayOneShot(s.clip);
    }

    public void StopMusic(string name)
    {
        Sound s = musicTracks.Find(sound => sound.name == name);
        if (s == null)
        {
            Debug.LogWarning("Music: " + name + " not found!");
            return;
        }
        s.source.Stop();
    }

    public void StopAllMusic()
    {
        foreach (Sound s in musicTracks)
        {
            s.source.Stop();
        }
    }
}